package com.edgardeng.generator.entity;

import lombok.Data;

import java.util.HashMap;

@Data
public class SQLDataSource {
    /**
     * 数据库名
     */
    private String name;
    /**
     * 服务器地址
     */
    private String host;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 驱动器
     */
    private String driver = "com.mysql.jdbc.Driver";
    /**
     * 驱动类别
     */
    private String driverType = "jdbc";
    /**
     * 类型
     */
    private String type = "mysql";

    private String tables;
    private String dir;
    private String packageName;
    private String frontDir;
    private String author = "";

    public String sql4QueryTables() {
        String sql = null;
        if ("mysql".equals(this.type)) {
            sql = "select table_name, engine, table_comment, create_time from information_schema.TABLES where TABLE_SCHEMA = '" + name + "'";
        } else if ("oracle".equals(type)) {
            sql = "select table_name from all_tables where owner='" + getName() + "'";
        } else if ("mssql".equals(type)) {
            // SELECT table_name = T.name , T.create_date ,T.modify_date FROM sys.tables AS T
            sql = "SELECT table_name = T.name, create_time = T.create_date FROM sys.tables AS T";
        }
        return sql;
    }

    public String sql4QueryColumns(String table) {
        String sql = null;
        if ("mysql".equals(this.type)) {
            sql = "select column_name, CASE COLUMN_key WHEN 'PRI' THEN 1 ELSE 0 END isKey,column_comment, data_type , column_key, extra from information_schema.COLUMNS where TABLE_SCHEMA = '" + name + "' and TABLE_NAME = '" + table + "'";
        } else if ("oracle".equals(type)) {
            sql = "select column_name, data_type from user_tab_columns where table_name = '" + table + "'";
        } else if ("mssql".equals(type)) {
            // select column_name,data_type from information_schema.columns where table_name = 'Sys_User'
            sql = "select column_name, data_type from information_schema.columns where table_name = '" + table + "'";
        } else {
            sql = "select column_name, column_comment, data_type from information_schema.COLUMNS where TABLE_SCHEMA = '" + name + "' and TABLE_NAME = '" + table + "'";
        }
        return sql;
    }

    public HashMap databaseConfig() {
        // 此处jdbc 应有变
        HashMap<String, String> object = new HashMap<>();
        object.put("password", password);
        object.put("driver", driver);
        if (this.type.equals("mysql")) {
            object.put("jdbcurl", "jdbc:mysql://" + host + "/" + name + "?useUnicode=true&characterEncoding=UTF-8" );
        } else if (this.type.equals("oracle")) {
            object.put("jdbcurl", "jdbc:oracle:thin:@" + host);
        } else if (this.type.equals("mssql")) {
            // "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tempdb";
            object.put("jdbcurl", "jdbc:sqlserver://" + host + ";DatabaseName=" + name);
        } else {
            object.put("jdbcurl", "jdbc:mysql://" + host + "/" + name +"?useUnicode=true&characterEncoding=UTF-8" );
        }

        object.put("username", username);
        object.put("aggregateProvider", "true");
        return object;
    }

}
